package com.fudan.adse.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间处理工具
 * @author Jerry Zhang
 * create: 2022-11-24 15:26
 */
public class DateTimeUtil {
    private static final String DATE_FORMAT_WITH_DETAIL = "yyyy-MM-dd HH:mm:ss";

    private static final String DATE_FORMAT = "yyyy-MM-dd";

    public static Date stringToDate(String date) {
        Date result = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT_WITH_DETAIL);
        try {
            result = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String format(Date date) {
        if (date == null) {
            return "";
        }
        return new SimpleDateFormat(DATE_FORMAT_WITH_DETAIL).format(date);
    }

    /**
     * 将 date 加上 minutes 秒
     * @param date
     * @param minutes
     * @return
     */
    public static Date datePlusMinutes(Date date, int minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minutes);
        return calendar.getTime();
    }
}
